home *** CD-ROM | disk | FTP | other *** search
-
- /*Preferences file handling*/
-
- /*This is a stripped-down preference file handling unit. It assumes that you always want your*/
- /*preference file in the Preferences folder, and use resources for storing the information.*/
- /*This file works with System 7 only!*/
-
- #include "MiniPreferences.h"
-
- /* 1) Find the Preferences folder*/
- /* 2) Check if the prefs file already exists. If it does, open it and exit. */
- /* 3) Create the prefs file and open it.*/
-
- Boolean SetPrefFile(Str255 prefsFileName, OSType prefCreator, OSType prefType, short *appFile, short *prefFile)
- {
- OSErr error;
- long directoryID;
- short vRefNum;
- FSSpec fileSpec;
- Boolean returnValue = false;
-
- *appFile = CurResFile (); /*spara programmerts resursfilreferens*/
- *prefFile = 0;
- /* 1) find the Preference folder*/
- error = FindFolder(kOnSystemDisk, kPreferencesFolderType, true, &vRefNum, &directoryID);
- /*Make a file spec to the pref folder*/
- error = FSMakeFSSpec(vRefNum, directoryID, prefsFileName, &fileSpec);
-
- /* 2) Check if a prefs file exists. If it does, open it and return it. */
- *prefFile = FSpOpenResFile(&fileSpec, fsRdWrPerm);
- if ( ResError () == noErr )
- {
- /* Open succeeded. */
- }
- else
- {
- *prefFile = 0;
-
- /* 3) Create prefs file and open it.*/
- /*Create the file*/
- error = FSpCreate(&fileSpec, prefCreator, prefType, 0); /*smRoman = 0*/
- if ( error == noErr )
- {
- /*Make a resource fork for it*/
- FSpCreateResFile(&fileSpec, prefCreator, prefType, 0); /*smRoman = 0*/
- if ( ResError () != noErr )
- {
- /*Error: Couldn't create resource fork*/
- }
- else
- /*Open it*/
- *prefFile = FSpOpenResFile(&fileSpec, fsRdWrPerm);
-
- if ( ResError () == noErr )
- return true; /* new pref file! */
- } /*Create succeeded*/
- else
- {
- /*Error: Failed to create prefsfile*/
- }
- } /*No prefs file existed*/
- return returnValue;
- } /*SetPrefFile*/
-
-
- /*********** CopyResource ***********/
-
- /* Error handling macros for CopyResource. */
- /* If error, set back to the previous resource file and return */
-
- #define CleanupAndExit {UseResFile(oldResourceFile);return ResError();}
- #define CheckError {if ( ResError()!= noErr) CleanupAndExit}
-
- /* CopyResource: Copy a resource from one resfile to another: */
- OSErr CopyResource(short fromFile, short toFile, ResType theResType, short id)
- {
- short oldResourceFile;
- Handle theResource, theResourceCopy;
- Boolean wasLoaded;
- short theID;
- ResType theType;
- Str255 theName;
- Boolean returnValue = noErr;
-
- /*Save the current resource file*/
- oldResourceFile = CurResFile ();
- UseResFile(fromFile);
- CheckError;
-
- SetResLoad(false);
- theResource = Get1Resource(theResType, id);
- /*Don't CheckError before doing SetResLoad(true)!!!*/
- SetResLoad(true);
- CheckError;
- if ( theResource != nil )
- {
- wasLoaded = (*theResource == nil);
- LoadResource(theResource);
- CheckError;
- UseResFile(toFile);
- CheckError;
- GetResInfo(theResource, &theID, &theType, theName);
- CheckError;
- theResourceCopy = theResource;
- if ( HandToHand(&theResourceCopy) != noErr )
- CleanupAndExit;
- CheckError;
- AddResource(theResourceCopy, theResType, id, theName);
- CheckError;
- ReleaseResource(theResourceCopy);
- if ( ! wasLoaded )
- ReleaseResource(theResource); /*If it wasn't loaded before, it shouldn't be afterwards.*/
- CheckError;
- returnValue = noErr;
- }
- else
- {
- returnValue = resNotFound;
- };
- UseResFile(oldResourceFile);
- return returnValue;
- } /*CopyResource*/
-